/**
 * @ClassName LC121 买卖股票的最佳时机
 * @Author ykx
 * @Date 2024/6/29 9:42
 * @Version 1.0
 */
public class LC121 {

    public static void main(String[] args) {
        LC121 lc121 = new LC121();
        System.out.println(lc121.maxProfit(new int[]{7, 1, 5, 3, 6, 4}));
    }

    public int maxProfit(int[] prices) {
        int minPrice = Integer.MAX_VALUE;
        int maxProfit = 0;
        for (int i = 0; i < prices.length; i++) {
            if (prices[i] < minPrice) {
                minPrice = prices[i];
            } else {
                maxProfit = Math.max(maxProfit, prices[i] - minPrice);
            }
        }
        return maxProfit;
    }
}
